package leetcode;

import java.util.LinkedList;

public class LC155 {

    LinkedList<Integer> stack ;
    int minVal ;

    public LC155() {
        stack = new LinkedList<>();
        minVal = Integer.MAX_VALUE;
    }

    public void push(int val) {
        stack.addLast(val);
        if (val < minVal)
            minVal = val;
    }

    public void pop() {
        int val = stack.getLast();
        if (val == minVal) {
            int newMinVal = Integer.MAX_VALUE;
            for (int i = 0; i < stack.size() - 1; i++) {
                if (newMinVal > stack.get(i))
                    newMinVal = stack.get(i);
            }
            minVal = newMinVal;
        }
        stack.removeLast();
    }

    public int top() {
        return stack.getLast();
    }

    public int getMin() {
        return minVal;
    }
}

